package com.scripts;

import Utilities.Commons;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

/**
 * Created by orlandocampos on 10/29/14.
 */
public class ExplicitWait {

    protected static WebDriver wd;
    protected static String url;
    protected static WebElement searchBar;

    @BeforeClass
    public void setup(){
        wd = new ChromeDriver();
        url = "https://www.google.com/";

        // Implicit WAIT
        //wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @Test
    public void explicitWait() throws Exception{
        wd.get(url);

        searchBar = wd.findElement(By.id("gbqfq"));
        searchBar.sendKeys("Pluralsight");

        /////////////////////////////////////// Explicit WAIT

        (wd.findElement(By.id("gbqfb"))).click();

        WebElement myDynamicElement2 = (new WebDriverWait(wd, 10))
                .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".q.qs")));

        //////////////////////////////////////////////////////////////////////////////////

        (wd.findElements(By.cssSelector(".q.qs"))).get(2).click();


        WebElement myDynamicElement3 = (new WebDriverWait(wd, 10))
                .until(ExpectedConditions.elementToBeClickable(By.cssSelector("#rg_s>div")));

        (wd.findElements(By.cssSelector("#rg_s>div"))).get(0).click();

        Commons.wait(Commons.TIMEOUT_5_SECONDS);

    }

    @AfterTest
    public void finish(){
        wd.close();
        wd.quit();
    }

}
